//
//  ToolBarBtn.swift
//  Yidingzhi
//
//  Created by flybywind on 15/12/2.
//  Copyright © 2015年 flybywind. All rights reserved.
//

import UIKit


protocol ToolBarDelegate {
    func toolBarTaped(fromBtn:ToolBarBtn)
}

class ToolBarBtn: UIView {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    var innerBtn : UIButton
    var info:String
    var delegate: ToolBarDelegate?
    var optionList : OptionsWrapper?
    
    var borderColor, borderColor2, backColor, backColor2: UIColor
    
    init(frame: CGRect, title:String, info:String,
            borderColor: UIColor, backColor: UIColor,
        borderColor2: UIColor, backColor2:UIColor) {
        self.info = info
        self.borderColor = borderColor
        self.borderColor2 = borderColor2
        self.backColor = backColor
        self.backColor2 = backColor2
        let rect = CGRect(x: 3, y: 3, width: frame.width-6, height: frame.height-6)
        innerBtn = UIButton(frame: rect)

        innerBtn.setTitle(title, forState: .Normal)
        innerBtn.layer.borderWidth = 1
        super.init(frame: frame)
        self.layer.borderWidth = 1
        self.normalColor()
        self.addSubview(innerBtn)
    }
    
    func setNormal() {
        self.normalColor()
    }
    
    func setActive() {
        self.activeColor()
    }
    func addTarget(target: ToolBarDelegate, forControlEvents controlEvents: UIControlEvents) {
        self.delegate = target
        innerBtn.addTarget(self, action: "callDelegate", forControlEvents: controlEvents)
    }
    
    func resetFrame(frame: CGRect){
        self.frame = frame
        innerBtn.frame = CGRect(x: 3, y: 3,
            width: frame.width-6, height: frame.height-6)
    }
    func callDelegate() {
        self.delegate?.toolBarTaped(self)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func activeColor() {
        self.innerBtn.setTitleColor(self.borderColor2, forState: .Normal)
        self.innerBtn.layer.borderColor = self.borderColor2.CGColor
        self.innerBtn.layer.backgroundColor = self.backColor2.CGColor
        self.layer.borderColor = self.borderColor2.CGColor
    }
    
    private func normalColor() {
        self.innerBtn.setTitleColor(self.borderColor, forState: .Normal)
        self.innerBtn.layer.borderColor = self.borderColor.CGColor
        self.innerBtn.layer.backgroundColor = self.backColor.CGColor
        self.layer.borderColor = self.borderColor.CGColor
    }
    
}
